package com.blog.user.controller;

import com.blog.response.Result;
import com.blog.user.pojo.AuthUser;
import com.blog.user.service.UserService;
import com.blog.user.util.SysUtils;
import com.blog.user.util.TokenDecode;
import com.sun.xml.bind.v2.model.core.ID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/authUser")
public class AuthUserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    @PostMapping("/ddd")
    public Result ddd() {
        // 获取登录用户信息
        //Map<String, String> userInfo = TokenDecode.getUserInfo();
        try {
            String userName = SysUtils.getUserName();
            String id = userService.findByUsername(userName).getId();
            Map<String, String> map = new HashMap<>();
            map.put("id", id);
            map.put("userName", userName);
            return Result.ok(map);
        } catch (Exception e) {
            return Result.error("未登录！");
        }
    }

    @GetMapping("/findById")
    public Result findById(String id) {
        AuthUser authUser = userService.findById(id);
        return Result.ok(authUser);
    }

    /**
     * 用户注册
     * 新增用户
     * 编辑用户信息
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody AuthUser authUser) {
        boolean isAdd = StringUtils.isBlank(authUser.getId());
        if (isAdd) {
            // 获取登录用户信息
            //Map<String, String> userInfo = TokenDecode.getUserInfo();
            AuthUser user = userService.findByUsername(authUser.getUsername());
            // 如果存在抛出异常提示用户名已存在
            if (user != null) {
                return Result.error("该用户名已存在！");
            }
            String password = authUser.getPassword();
            if (StringUtils.isBlank(password)) {
                return Result.error("密码不能为空！");
            }
            // 2.发送短信 1）注册成功发送短信提示注册成功 2）注册账号发送短信验证码
            // 3.注册成功
            userService.add(authUser);
        } else {
            userService.update(authUser);
        }
        return Result.ok(isAdd ? "注册成功！" : "更新成功!", authUser.getId());
    }

    /**
     * 查询用户
     * @param page
     * @param size
     * @param query
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(@RequestParam("page") int page, @RequestParam("size") int size, @RequestParam("query") String query) {
        List<AuthUser> authUser = userService.findAll(page, size, query);
        return Result.ok(authUser);
    }

    /**
     * 停用账号
     * @return
     */
    @GetMapping("/shopUser")
    public Result stopUser(String id) {
        userService.stopUser(id);
        return Result.ok("操作成功！");
    }

    /**
     * 注销账号
     * @return
     */
    @GetMapping("/cancelUser")
    public Result cancelUser(String id) {
        userService.cancelUser(id);
        return Result.ok("操作成功！");
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(String[] ids) {
        for (String id : ids) {
            userService.deleteById(id);
        }
        return Result.ok("操作成功！");
    }
}
